package cn.hf.mychat.TypeHandlerUtils;

import lombok.Setter;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@Setter
public class TypeHandler extends BaseTypeHandler<String> {


    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, String s, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i,EncryptUtils.encrypt(s));
    }

    @Override
    public String getNullableResult(ResultSet resultSet, String s) throws SQLException {
        String r = resultSet.getString(s);
        return r == null ? null : EncryptUtils.decrypt(r);
    }

    @Override
    public String getNullableResult(ResultSet resultSet, int i) throws SQLException {
        String r = resultSet.getString(i);
        return r == null ? null : EncryptUtils.decrypt(r);
    }

    @Override
    public String getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        String r = callableStatement.getString(i);
        // 兼容待修复的数据
        return r == null ? null : EncryptUtils.decrypt(r);
    }
}
